package app

import (
	"github.com/go-apibox/api"
)

const (
	SESSION_NAME = "default"
)

type MySession struct {
	session *api.Session
}

func GetSession(c *api.Context) (*MySession, *api.Error) {
	s, err := c.Session(SESSION_NAME)
	if err != nil {
		return nil, c.Error.New(api.ErrorInternalError, "SessionFailed").SetMessage(err.Error())
	}
	return &MySession{session: s}, nil
}

func (my *MySession) GetUserId() uint32 {
	return my.session.Values["UserId"].(uint32)
}

func (my *MySession) GetUserName() string {
	return my.session.Values["UserName"].(string)
}

func (my *MySession) GetParentId() uint32 {
	return my.session.Values["ParentId"].(uint32)
}
func (my *MySession) GetUserGroupId() uint32 {
	return my.session.Values["UserGroupId"].(uint32)
}

func (my *MySession) GetUserType() string {
	return my.session.Values["UserType"].(string)
}

func (my *MySession) IsAdmin() bool {
	return my.session.Values["UserType"].(string) == USER_TYPE_ADMIN
}

func (my *MySession) IsPersonnel() bool {
	return my.session.Values["UserType"].(string) == USER_TYPE_PERSONNEL
}

func (my *MySession) IsUser() bool {
	return my.session.Values["UserType"].(string) == USER_TYPE_USER
}

func NotAdminAddParamUserId(c *api.Context, params *api.Params) *api.Error {
	session, apiErr := GetSession(c)
	if apiErr != nil {
		return apiErr
	}
	if !session.IsAdmin() {
		params.Set("UserId", session.GetUserId())
	}
	return nil
}
func AddParamUserId(c *api.Context, params *api.Params) *api.Error {
	session, apiErr := GetSession(c)
	if apiErr != nil {
		return apiErr
	}
	params.Set("UserId", session.GetUserId())
	return nil
}
